import axios from "axios";
import { UserModel } from "../model/User";
import { CartsModel } from "../model/Carts";
import { CommodityModel } from "../model/Commodity";
import { OrdersModel } from "../model/Orders";


export function queryUser(phone: string): Promise<UserModel> {
    const promise: Promise<UserModel> = new Promise<UserModel>((resolve, reject) => {
        axios.get(`/api/user/query?phone=${phone}`)
            .then((_reslut) => {
                if (_reslut.data.code === 200) {
                    resolve(_reslut.data.data);
                } else
                    reject(_reslut.data);
            })
            .catch(() => {
                reject(null);
            })
    });
    return promise;
}

export function getIp(): Promise<string> {
    const promise: Promise<string> = new Promise<string>((resolve, reject) => {
        axios.get('https://ip.useragentinfo.com/json?ip=')
            .then((_reslut) => {
                resolve(_reslut.data.ip);
            })
            .catch(() => {
                reject('');
            })
    });
    return promise;
}

export function getProvince(): Promise<string> {
    const promise: Promise<string> = new Promise<string>((resolve, reject) => {
        axios.get('https://ip.useragentinfo.com/json?ip=')
            .then((_reslut) => {
                resolve(_reslut.data.province);
            })
            .catch(() => {
                reject('');
            })
    });
    return promise;
}

export function userRegister(user: UserModel): Promise<number> {
    const promise: Promise<number> = new Promise<number>((resolve, reject) => {
        axios.post('/api/user/register', user)
            .then((_reslut) => {
                resolve(_reslut.data.code)
            })
            .catch(() => {
                reject('');
            })
    });
    return promise;
}

export function createOrder(order: OrdersModel): Promise<object> {
    const promise: Promise<object> = new Promise<object>((resolve, reject) => {
        axios.post('/api/orders/create', order)
            .then((_reslut) => {
                resolve(_reslut.data)
            })
            .catch(() => {
                reject('');
            })
    });
    return promise;
}

export function addCommodity(commodity: CommodityModel): Promise<object> {
    const promise: Promise<object> = new Promise<object>((resolve, reject) => {
        axios.post('/api/commodity/add', commodity)
            .then((_reslut) => {
                resolve(_reslut.data)
            })
            .catch(() => {
                reject('');
            })
    });
    return promise;
}

export async function queryAllCommodity() {
    let reslut = await axios.get(`/api/commodity/all`)
    return reslut.data;
}

export async function pay(oid: number) {
    let reslut = await axios.get(`/api/orders/pay?oid=${oid}`)
    return reslut.data;
}

export async function queryCurrentCommodity() {
    let reslut = await axios.get(`/api/commodity/current`)
    return reslut.data;
}

export async function queryAllKind() {
    let reslut = await axios.get(`/api/kind/all`)
    return reslut.data;
}

export async function queryMessage(uid: number) {
    let reslut = await axios.get(`/api/message/unread?uid=${uid}`)
    return reslut.data;
}

export async function readMessage(mid: number) {
    let reslut = await axios.get(`/api/message/read?mid=${mid}`)
    return reslut.data;
}

export async function recommendCommodity() {
    let reslut = await axios.get(`/api/commodity/all`)
    return reslut.data;
}

export async function queryRecommendImage() {
    let reslut = await axios.get(`/api/image/recommend`)
    return reslut.data;
}

export async function queryCommodityByName(name: string) {
    let reslut = await axios.get(`/api/commodity/name?name=${name}`)
    return reslut.data;
}

export async function queryPaid(uid: number) {
    let reslut = await axios.get(`/api/orders/paid?uid=${uid}`)
    return reslut.data;
}

export async function queryUnPaid(uid: number) {
    let reslut = await axios.get(`/api/orders/unPaid?uid=${uid}`)
    return reslut.data;
}

export async function queryCommodityByType(type: string) {
    let reslut = await axios.get(`/api/commodity/type?type=${type}`)
    return reslut.data;
}

export async function queryCommodityByGid(gid: number) {
    let reslut = await axios.get(`/api/commodity/gid?gid=${gid}`)
    return reslut.data.data;
}

export async function queryStoreByUid(uid: string) {
    let reslut = await axios.get(`/api/store/uid?uid=${uid}`)
    return reslut.data;
}

export async function transitOrder(uid: number) {
    let reslut = await axios.get(`/api/orders/transit?uid=${uid}`)
    return reslut.data;
}

export async function receiveOrder(uid: number) {
    let reslut = await axios.get(`/api/orders/receive?uid=${uid}`)
    return reslut.data;
}

export async function cancelOrder(oid: number) {
    let reslut = await axios.get(`/api/orders/cancel?oid=${oid}`)
    return reslut.data;
}

export async function finishOrder(oid: number) {
    let reslut = await axios.get(`/api/orders/finish?oid=${oid}`)
    return reslut.data;
}

export async function browse(gid: number) {
    let reslut = await axios.get(`/api/commodity/browse?gid=${gid}`)
    return reslut.data;
}

export async function queryCommodityByUid(uid: number) {
    let reslut = await axios.get(`/api/commodity/uid?uid=${uid}`)
    return reslut.data;
}

export async function deleteCommodity(gid: number) {
    let reslut = await axios.get(`/api/commodity/delete?gid=${gid}`)
    return reslut.data;
}

export async function pauseCommodity(gid: number) {
    let reslut = await axios.get(`/api/commodity/pause?gid=${gid}`)
    return reslut.data;
}

export async function queryDefaultAddressByUid(uid: number) {
    let reslut = await axios.get(`/api/address/default?uid=${uid}`)
    return reslut.data;
}

export async function updateDefaultAddressByUid(uid: number, aid: number) {
    let reslut = await axios.get(`/api/address/setDefault?aid=${aid}&uid=${uid}`)
    return reslut.data;
}

export async function queryAllCartsByUid(uid: number) {
    let reslut = await axios.get(`/api/carts/queryAllCarts?uid=${uid}`)
    return reslut.data;
}

export async function queryCartsByCid(cid: number) {
    let reslut = await axios.get(`/api/carts/queryCarts?cid=${cid}`)
    return reslut.data;
}

export async function removeCartsByCid(cid: number) {
    let reslut = await axios.get(`/api/carts/delete?cid=${cid}`)
    return reslut.data;
}

export async function updateCartsByCid(cid: number, count: number, price: number) {
    let reslut = await axios.get(`/api/carts/updateCarts?count=${count}&price=${price}&cid=${cid}`)
    return reslut.data;
}

export async function updateUser(info: object) {
    let reslut = await axios.post(`/api/user/update`, info)
    return reslut.data;
}

export async function addAddress(address: object) {
    let reslut = await axios.post(`/api/address/add`, address)
    return reslut.data;
}

export async function queryAddressByUid(uid: number) {
    let reslut = await axios.get(`/api/address/query?uid=${uid}`)
    return reslut.data;
}

export function addMCarts(cart: CartsModel): Promise<number> {
    const promise: Promise<number> = new Promise<number>((resolve, reject) => {
        axios.post('/api/carts/addCarts', cart)
            .then((_reslut) => {
                resolve(_reslut.data.code)
            })
            .catch(() => {
                reject('');
            })
    });
    return promise;
}

export function applyStore(store: object): Promise<object> {
    const promise: Promise<object> = new Promise<object>((resolve, reject) => {
        axios.post('/api/store/apply', store)
            .then((_reslut) => {
                resolve(_reslut.data)
            })
            .catch(() => {
                reject('');
            })
    });
    return promise;
}

export async function adminApplyStore() {
    let reslut = await axios.get(`/api/store/verify`)
    return reslut.data;
}

export async function adminApplyCommodity() {
    let reslut = await axios.get(`/api/commodity/verify`)
    return reslut.data;
}

export async function authCommodity(id: number) {
    let reslut = await axios.get(`/api/commodity/auth?gid=${id}`)
    return reslut.data;
}

export async function refuseCommodity(id: number) {
    let reslut = await axios.get(`/api/commodity/refuse?gid=${id}`)
    return reslut.data;
}

export async function authStore(id: number) {
    let reslut = await axios.get(`/api/store/auth?sid=${id}`)
    return reslut.data;
}

export async function refuseStore(id: number) {
    let reslut = await axios.get(`/api/store/refuse?sid=${id}`)
    return reslut.data;
}